package com.ewing.web.action;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.ewing.busi.seller.model.SellerShop;
import com.ewing.busi.seller.service.SellerShopService;
import com.ewing.busi.stat.service.MainStatService;
import com.ewing.busi.system.model.SysMenu;
import com.ewing.busi.system.service.MenuService;
import com.ewing.core.app.action.base.BaseAction;
import com.ewing.core.app.bean.TreeObject;
import com.ewing.core.jdbc.DaoException;

/**
 * 后台管理主頁展示类
 * 
 * @author tanson lam
 * @creation 2015年1月10日
 */

public class MainAction extends BaseAction {
    private static Logger logger = Logger.getLogger(MainAction.class);
    private static final String MAIN_PAGE = "/admin/main.html";
    private static final String DASHBOARD_PAGE = "/admin/dashboard.html";
    @Resource
    private MenuService menuService;
    @Resource
    private MainStatService mainStatService;
    @Resource
    private SellerShopService sellerShopService;

    public void show() throws DaoException, IllegalAccessException, InvocationTargetException {
        Map<String, Object> dataModel = new HashMap<String, Object>();
        List<TreeObject> menuList = menuService.queryModuleTree(null);
        SysMenu firstTopmenu = menuService.queryFirstTopMenu();
        TreeObject parentMenu = new TreeObject();
        TreeObject menu = new TreeObject();
        String parentMenuId = request.getParameter("parentMenuId");
        String menuId = request.getParameter("menuId");
        for (TreeObject m : menuList) {
            if (parentMenuId != null && m.getId().equals(Integer.valueOf(parentMenuId)))
                BeanUtils.copyProperties(parentMenu, m);
            if (menuId != null && m.getId().equals(Integer.valueOf(menuId)))
                BeanUtils.copyProperties(menu, m);
        }

        request.getSession().setAttribute("parentMenu", parentMenu);
        request.getSession().setAttribute("curMenu", menu);
        dataModel.put("menuList", menuList);
        dataModel.put("firstTopmenuId", firstTopmenu != null ? firstTopmenu.getId() : 0);
        renderWithHead(MAIN_PAGE, dataModel);
    }

    public void dashboard() {
        Map<String, Object> dataModel = new HashMap<String, Object>();

        try {
            Float totalIncome = mainStatService.countIncomeHistory(getLoginUserId());
            Integer waitProOrderCount = mainStatService.countWaitProOrder(getLoginUserId());
            SellerShop shop = sellerShopService.findSellerShop(getLoginUserId());
            if (shop != null) {
                Integer yesterDayAccessCount = mainStatService
                        .getYesterdayAccessCount(shop.getId());
                dataModel.put("yesterDayAccessCount", yesterDayAccessCount);
            }
            dataModel.put("totalIncome", totalIncome);
            dataModel.put("waitProOrderCount", waitProOrderCount);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        render(DASHBOARD_PAGE, dataModel);
    }
}
